home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 15324 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.3 KB  |  66 lines

  1. Path: ub239.dialup.uwa.edu.au!localhost!prye
  2. From: prye@cyllene.uwa.edu.au (Peter Rye)
  3. Newsgroups: comp.sys.lang.c++,comp.lang.c++
  4. Subject: Re: Help needed in C++ Function templates
  5. Date: 04 Apr 1996 18:06:46 GMT
  6. Organization: The University of Western Australia
  7. Message-ID: <PRYE.96Apr5020646@cyllene.uwa.edu.au>
  8. References: <NEWTNews.828668110.2725.seedy@trigent.trigent.com>
  9. NNTP-Posting-Host: ub239.dialup.uwa.edu.au
  10. In-reply-to: seedy@ultranet.com's message of Thu, 04 Apr 96 17:29:36 PDT
  11. X-Mailer: GNU Emacs 19.28
  12.  
  13. >>>>> "seedy" == seedy  <seedy@ultranet.com> writes:
  14.  
  15.     seedy> When attempted to compile the following code using GNU's
  16.     seedy> g++ (ver. 2.7.1) on HP-UX, I got the following error :
  17.  
  18.     seedy> news.cc: In function `int equal(const class abc &, const
  19.     seedy> class abc &)': news.cc:23: no match for `operator ==(class
  20.     seedy> abc, class abc)'
  21.  
  22.  
  23.     seedy> #include <iostream.h>
  24.  
  25.     seedy> class abc { 
  26.     seedy>    public : 
  27.     seedy>       abc( int x ) { a = x; b = 0; } 
  28.     seedy>       int a;
  29.     seedy>       int b; 
  30.     seedy>  inline int operator ==(abc x) { 
  31.                return ((a==x.a) && (b==x.b)); 
  32.     seedy>   } 
  33.     seedy> };
  34.  
  35.     seedy> template < class Element> 
  36.     seedy> inline int equal (Element const& e1, Element const& e2) { 
  37.     seedy>           return e1 == e2;
  38.     seedy> }
  39.  
  40.     seedy> main() { 
  41.     seedy>    abc a1(5), a2(5);
  42.     seedy> cout << "Return value is = " << equal(a1, a2) << endl; }
  43.  
  44.     seedy> Could any one explain why this error happens.  Please send
  45.     seedy> your response via mail to 'seedy@trigent.com' or post it
  46.     seedy> here.
  47.  
  48. My compiler gives the following error.
  49.  
  50. ub239:~$ g++ -Wall -o check check.cc
  51. check.cc: In function `int equal(const class abc &, const class abc &)':
  52. check.cc:14: non-const member function `abc::operator ==(abc)'
  53. check.cc:24: called for const object at this point in file
  54.  
  55. Making int operator == (abc) a constant member function -> compiles and 
  56. runs without error.
  57. ie:
  58.  inline int operator ==(abc x) const { 
  59.                return ((a==x.a) && (b==x.b)); 
  60.         }
  61. --
  62. Peter Rye   prye@cyllene.uwa.edu.au,  prye@ichr.uwa.edu.au
  63. Respiratory Research Fellow, Princess Margaret Hospital for Children
  64. Perth, Western Australia    Ph: +61 (09) 340 8985, Fax: +61 (09) 388 2097
  65. ** Smoking areas in restaurants are like peeing areas in swimming pools. **
  66.